package org.anyin.gitee.cloud.center.upms.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.anyin.gitee.cloud.center.upms.convert.SysRoleConvert;
import org.anyin.gitee.cloud.center.upms.enums.UpmsExCodeEnum;
import org.anyin.gitee.cloud.center.upms.model.SysRole;
import org.anyin.gitee.cloud.center.upms.model.SysRoleMenu;
import org.anyin.gitee.cloud.center.upms.repository.SysRoleMenuRepository;
import org.anyin.gitee.cloud.center.upms.repository.SysRoleRepository;
import org.anyin.gitee.cloud.center.upms.service.SysMenuService;
import org.anyin.gitee.cloud.center.upms.service.SysRoleService;
import org.anyin.gitee.cloud.center.upms.service.dto.SysMenuInfoDTO;
import org.anyin.gitee.cloud.center.upms.service.dto.SysRoleInfoDTO;
import org.anyin.gitee.cloud.center.upms.service.dto.SysRoleSaveDTO;
import org.anyin.gitee.cloud.lib.core.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleRepository sysRoleRepository;

    @Autowired
    private SysRoleMenuRepository sysRoleMenuRepository;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleConvert sysRoleConvert;

    @Override
    public SysRoleRepository getRepository() {
        return sysRoleRepository;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class, timeout = 60)
    public void create(SysRoleSaveDTO info) {
        SysRole exist = sysRoleRepository.getByName(info.getRoleName());
        if(exist != null){
            throw UpmsExCodeEnum.ROLE_NAME_EXIST.getException();
        }
        exist = sysRoleRepository.getByTitle(info.getRoleTitle());
        if(exist != null){
            throw UpmsExCodeEnum.ROLE_TITLE_EXIST.getException();
        }
        SysRole entity = new SysRole();
        entity.setRoleName(info.getRoleName());
        entity.setDescription(info.getDescription());
        entity.setRoleTitle(info.getRoleTitle());
        sysRoleRepository.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class, timeout = 60)
    public void modify(Long id, SysRoleSaveDTO info) {
        SysRole exist = sysRoleRepository.getByName(info.getRoleName());
        if(exist != null && !exist.getId().equals(id)){
            throw UpmsExCodeEnum.ROLE_NAME_EXIST.getException();
        }
        exist = sysRoleRepository.getByTitle(info.getRoleTitle());
        if(exist != null && !exist.getId().equals(id)){
            throw UpmsExCodeEnum.ROLE_TITLE_EXIST.getException();
        }
        SysRole entity = new SysRole();
        entity.setRoleName(info.getRoleName());
        entity.setDescription(info.getDescription());
        entity.setId(id);
        entity.setRoleTitle(info.getRoleTitle());
        sysRoleRepository.updateById(entity);
    }

    @Override
    public SysRoleInfoDTO info(Long id) {
        SysRole role = sysRoleRepository.getById(id);
        if(role == null){
            throw UpmsExCodeEnum.ROLE_NOT_FOUND.getException();
        }

        List<String> menuCodeList = sysRoleMenuRepository.getMenuCodeListByRoleId(id);

        SysRoleInfoDTO info = new SysRoleInfoDTO();
        info.setId(role.getId());
        info.setRoleName(role.getRoleName());
        info.setDescription(role.getDescription());
        info.setRoleTitle(role.getRoleTitle());
        info.setMenuCodeList(menuCodeList);
        return info;
    }

    @Override
    public void delete(Long id) {
        sysRoleRepository.removeById(id);
    }

    @Override
    public Page<SysRoleInfoDTO> page(Integer pageNo, Integer pageSize, String roleName) {
        Page<SysRole> page = new Page<>(pageNo, pageSize);
        page = sysRoleRepository.page(page, roleName);
        return sysRoleConvert.getSysRolePageDTO(page);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class, timeout = 60)
    public void auth(Long id, List<String> menuCodeList) {
        sysRoleMenuRepository.deleteByRoleId(id);
        if(CollectionUtil.isEmpty(menuCodeList)){
            return;
        }
        List<SysMenuInfoDTO> menuList = sysMenuService.getByCodeList(menuCodeList);
        List<SysRoleMenu> list = Lists.newArrayList();
        for(SysMenuInfoDTO menu : menuList){
            SysRoleMenu entity = new SysRoleMenu();
            entity.setRoleId(id);
            entity.setMenuId(menu.getId());
            list.add(entity);
        }
        sysRoleMenuRepository.batchInsert(list);
    }
}
